package com.budwk.app.sqfwV2.service.entrepreneurShipHouse;


import com.budwk.app.sqfw.component.CommonComponent;
import com.budwk.app.sqfw.enums.SupportBusinessTypeEnum;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfw.vo.GenerateExcelUrlVO;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CompleteFlowPdfParamForController;
import com.budwk.app.sqfwDoc.dto.CompleteFlowTemplateCodeUpdateDTO;
import com.budwk.app.sqfwDoc.dto.TemplateCodeUpdateDTO;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownExcelLedgerReqVo;
import com.budwk.app.sqfwV2.dto.dto.dayingRoad.DownFileTypesResVo;
import com.budwk.app.sqfwV2.enums.EntrepreneurHouseFileEnum;
import com.budwk.app.sys.models.Sys_dict;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.app.util.poiUtils.CommonFormOfficeUtils;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@IocBean
@Slf4j
public class EntrepreneurShipHouseServiceImpl implements EntrepreneurShipHouseService{


    @Inject
    private EntrepreneurShipHousePdfService entrepreneurShipHousePdfService;
    @Inject
    private EntrepreneurShipHouseExcelService entrepreneurShipHouseExcelService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;
    @Inject
    private SysDictService sysDictService;

    @Override
    public void submitData(TemplateCodeUpdateDTO dto) {
        CompleteFlowPdfParamForController param = new CompleteFlowPdfParamForController();
        // 组装参数
        CompleteFlowTemplateCodeUpdateDTO codeUpdateDTO = new CompleteFlowTemplateCodeUpdateDTO();
        codeUpdateDTO.setCompleteFlow(dto.getCompleteFlow());
        codeUpdateDTO.setFormSummary(dto.getFormSummary());
        param.setCompleteFlowDataDTO(codeUpdateDTO);
        param.setUserId(SecurityUtil.getUserId());
        entrepreneurShipHousePdfService.createPdfAndMsg(param);
    }

    @Override
    public Result<?> reGeneratePDF(String sqfwFlowId) {
        CompleteFlowPdfParamForController param = new CompleteFlowPdfParamForController();
        param.setCompleteFlowId(sqfwFlowId);
        entrepreneurShipHousePdfService.reCreatePdfAndMsg(param);
        return Result.success();
    }

    @Override
    public Result<?> downExcelLedger(DownExcelLedgerReqVo reqVo) {
        List<ExcelCreateConfig> excelConfigList = CommonComponent.getExcelConfigList(reqVo);
        List<GenerateExcelUrlVO> generateExcelUrlVOS = entrepreneurShipHouseExcelService.generateExcel(excelConfigList, SecurityUtil.getUserId(), reqVo.getSqfwIds());
        return Result.data(generateExcelUrlVOS);
    }

    @Override
    public Result<?> listByConditionKey(String placeName) {
        List<Sqfw_formSummary_new> list = sqfwFormsummaryNewService.findByPlaceName(placeName,
                SupportBusinessTypeEnum.ENTERPRENEURAL.getName());
        return Result.data(list);
    }

    @Override
    public Result<List<DownFileTypesResVo>> listDownFileTypes(String sqfwId) {
        //2 按照社区服务id查询数据
        List<Sqfw_formSummary_new> list = sqfwFormsummaryNewService.queryBySqfwIdAndType(sqfwId, SupportBusinessTypeEnum.ENTERPRENEURAL.getName());
        // 检查数据是否可生成
        List<DownFileTypesResVo> results = new ArrayList<>();
        List<EntrepreneurHouseFileEnum> allExcel = EntrepreneurHouseFileEnum.getAllExcel();
        for (EntrepreneurHouseFileEnum fileEnum : allExcel) {
            getDownFileTypesResVo(results, list, fileEnum);
        }
        List<EntrepreneurHouseFileEnum> allPdf = EntrepreneurHouseFileEnum.getAllPdf();
        for (EntrepreneurHouseFileEnum fileEnum : allPdf) {
            Sys_dict dict = sysDictService.getByCode(fileEnum.getCode());
            DownFileTypesResVo resVo = new DownFileTypesResVo();
            resVo.setFileName(dict.getName() + ".pdf").
                    setFileCode(fileEnum.getCode())
                    .setHiveData(list.size() > 0)
                    .setCreatedAt(System.currentTimeMillis())
                    .setFileType("pdf");
            results.add(resVo);
        }
        return Result.data(results);
    }

    @Override
    public void downWordTemplate(HttpServletResponse response) {
        String fileName = SupportBusinessTypeEnum.ENTERPRENEURAL.getName() + ".docx";
        String filepath = CommonFormOfficeUtils.getTemplatePathByOS("sqfwV2/entrepreneurHouse/template/householdSurveyFormDocTemplate.docx");
        // 下载文件
        CommonComponent.downFile(response, filepath, fileName);
    }


    private void getDownFileTypesResVo(List<DownFileTypesResVo> results, List<Sqfw_formSummary_new> list, EntrepreneurHouseFileEnum fileEnum) {
        Sys_dict dict = sysDictService.getByCode(fileEnum.getCode());
        DownFileTypesResVo resVo = new DownFileTypesResVo();
        resVo.setFileCode(fileEnum.getCode()).setFileName(dict.getName())
                .setHiveData(list.size() > 0)
                .setFileType("excel")
                .setLocation(dict.getLocation());
        results.add(resVo);
    }
}
